const app = require('./src/app');
const { sequelize, testConnection } = require('./src/config/database');
require('./src/models'); // 加载模型关联

const PORT = process.env.PORT || 3001;

const startServer = async () => {
  try {
    // 测试数据库连接
    await testConnection();

    // 启动服务器
    app.listen(PORT, () => {
      console.log(`🚀 服务器运行在端口 ${PORT}`);
      console.log(`📍 环境: ${process.env.NODE_ENV || 'development'}`);
      console.log(`🔗 健康检查: http://localhost:${PORT}/health`);
    });
  } catch (error) {
    console.error('❌ 服务器启动失败:', error);
    process.exit(1);
  }
};

startServer();
